package com.xxg.renrenuser.controller;

import com.xxg.renrencommon.domain.result.BaseResult;
import com.xxg.renrenuser.dao.bean.ExpressAddressInfo;
import com.xxg.renrenuser.domain.query.AddAddressQuery;
import com.xxg.renrenuser.domain.query.DeleteAddressQuery;
import com.xxg.renrenuser.domain.query.EditAddressQuery;
import com.xxg.renrenuser.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 地址管理接口
 *
 * @author xxg
 * @date 2020/10/21
 */
@Api(tags = "地址管理接口")
@RestController
@RequestMapping("/address")
public class AddressController {
    private final AddressService addressService;

    public AddressController(AddressService addressService) {
        this.addressService = addressService;
    }

    @ApiOperation("添加地址")
    @RequestMapping(value = "addAddress", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "addAddressQuery", value = "地址参数", required = true, dataType = "AddAddressQuery")})
    public BaseResult<Void> addAddress(@RequestBody @Validated AddAddressQuery addAddressQuery, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        addAddressQuery.setUserId(userId);
        return addressService.addAddress(addAddressQuery);
    }

    @ApiOperation("编辑地址")
    @RequestMapping(value = "editAddress", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "editAddressQuery", value = "地址参数", required = true, dataType = "EditAddressQuery")})
    public BaseResult<Void> editAddress(@RequestBody @Validated EditAddressQuery editAddressQuery, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return addressService.editAddress(userId, editAddressQuery);
    }

    @ApiOperation("获取地址列表")
    @RequestMapping(value = "getAddressList", method = RequestMethod.GET)
    public BaseResult<List<ExpressAddressInfo>> getAddressList(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return addressService.getAddressList(userId);
    }

    @ApiOperation("删除地址")
    @RequestMapping(value = "deleteAddress", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "deleteAddressQuery", value = "地址ID", required = true, dataType = "DeleteAddressQuery")})
    public BaseResult<Void> deleteAddress(@RequestBody @Validated DeleteAddressQuery deleteAddressQuery) {
        return addressService.deleteAddress(deleteAddressQuery);
    }
}
